package cc.mrbird.febs.buss.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.buss.entity.MenuItemConn;
import cc.mrbird.febs.buss.service.IMenuItemConnService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Enumeration;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author Lujingyuan
 * @date 2021-10-08 17:33:12
 */
@Slf4j
@Validated
@Controller
public class MenuItemConnController extends BaseController {

    @Autowired
    private IMenuItemConnService menuItemConnService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuItemConn")
    public String menuItemConnIndex(){
        return FebsUtil.view("menuItemConn/menuItemConn");
    }

    @GetMapping("menuItemConn")
    @ResponseBody
    @RequiresPermissions("menuItemConn:list")
    public FebsResponse getAllMenuItemConns(MenuItemConn menuItemConn) {
        return new FebsResponse().success().data(menuItemConnService.findMenuItemConns(menuItemConn));
    }

    @GetMapping("menuItemConn/list")
    @ResponseBody
    public FebsResponse menuItemConnList(QueryRequest request, HttpServletRequest req, MenuItemConn menuItemConn) {
        Enumeration enu=req.getParameterNames();
        HashMap<String,String> map=new HashMap<String,String>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        Map<String, Object> dataTable = getDataTable(this.menuItemConnService.findMenuItemConnDetail(request, map));
        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuItemConn/add/{name}")
    public String goMenuItemConnAdd(@PathVariable String name,HttpServletRequest req) {
        //String name=req.getParameter("name");
        if(StringUtils.isNotBlank(name)){
            return FebsUtil.view("menuItemConn/menuItemConnAdd-"+name);
        }
        return FebsUtil.view("menuItemConn/menuItemConnAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuItemConn/update/{id}")
    public String goMenuItemConnUpdate(@PathVariable String id, Model model) {
        MenuItemConn obj= menuItemConnService.getById(id);
        model.addAttribute("obj",obj);
        return FebsUtil.view("menuItemConn/menuItemConnUpdate");
    }

    @ControllerEndpoint(operation = "新增MenuItemConn", exceptionMessage = "新增MenuItemConn失败")
    @PostMapping("menuItemConn")
    @ResponseBody
    public FebsResponse addMenuItemConn(HttpServletRequest req) {
        String bussIds=req.getParameter("bussIds");
        String bussTableName=req.getParameter("bussTableName");
        String menuId=req.getParameter("menuId");
        if(StringUtils.isNotBlank(bussIds)&&StringUtils.isNotBlank(bussTableName)&&StringUtils.isNotBlank(menuId)){
            String[] ids=bussIds.split(",");
            for(int i=0;i<ids.length;i++){
                Map<String,Object> map=new HashMap<>();
                map.put("bussId",ids[i]);
                map.put("bussTableName",bussTableName);
                map.put("menuId",menuId);
                List<MenuItemConn> connList=menuItemConnService.selectByMap(map);
                if(connList==null||connList.size()==0){
                    MenuItemConn menuItemConn=new MenuItemConn();
                    menuItemConn.setBussId(ids[i]);
                    menuItemConn.setBussTableName(bussTableName);
                    menuItemConn.setMenuId(menuId);
                    this.menuItemConnService.add(menuItemConn);
                }
            }
        }


        return new FebsResponse().success();
    }

    @GetMapping("menuItemConn/delete/{objIds}")
    @ControllerEndpoint(operation = "删除", exceptionMessage = "删除失败")
    @ResponseBody
    public FebsResponse deleteMenuItemConn( @PathVariable String objIds) {
        String[] ids = objIds.split(StringPool.COMMA);
        this.menuItemConnService.deleteMenuItemConn(ids);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改MenuItemConn", exceptionMessage = "修改MenuItemConn失败")
    @PostMapping("menuItemConn/update")
    @ResponseBody
    public FebsResponse updateMenuItemConn(MenuItemConn menuItemConn) {
        this.menuItemConnService.updateMenuItemConn(menuItemConn);
        return new FebsResponse().success();
    }
}
